#include "selectcpp_c.h"
#ifdef ECHO_C_C5_CPP
#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory.h>
#include <thread>

#include <WinSock2.h>
#include <WS2tcpip.h>
#include <windows.h>

void str_cli(SOCKET sockfd){
	char input[101], outcome[101];
	int n;
	while (gets(input) != nullptr){
		send(sockfd, input, strlen(input), 0);
		if ((n = recv(sockfd, outcome, 100, 0)) > 0){
			outcome[n] = 0;
			puts(outcome);
		}
		else{
			printf("server terminated\n");
			return;
		}
	}
}

int main(int argc, char *argv[]){
	WSAData wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);

	SOCKET sockfd;
	sockaddr_in servaddr;

	if (argc != 2){
		printf("arg count error");
		exit(0);
	}

	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	memset(&servaddr, 0, sizeof(sockaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(12000);
	inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	connect(sockfd, reinterpret_cast<sockaddr*>(&servaddr), sizeof(servaddr));
	str_cli(sockfd);
	return 0;
}

#endif